import { defineStore } from 'pinia'
import { getMyUser } from '../api/index'

function createLocalStoragePlugin() {
    return (use) => {
      // 使用定时器模拟监听localStorage变化
      setInterval(() => {
        const state = use()
        const localStorageValue = localStorage.getItem("myuser")
        if (localStorageValue !== null) {
          // 将localStorage的值反序列化并更新状态
        //   let data= JSON.parse(localStorageValue);
        //   state.$patch(() => {
        //     return {
        //         apiHost:data.apiHost,
        //         orgId:data.orgId
        //     }
        //   })
          state.$patch(() => {
            return JSON.parse(localStorageValue)
          })
        }
      }, 1000)
    }
  }

export const useUserStore = defineStore('user', {
    // persist: true, // 开启当前模块持久化
    // persist:{// 开启当前模块持久化
    //     enable:true,
    //     strategies: [ 
    //         { key: 'token',storage: localStorage}, 
    //     ],
    // },
    persist: {
        enabled: true, // 开启数据缓存
        strategies: [
          {
            key: 'myuser',
            storage: localStorage // 默认是sessionStorage
          }
        ],
    },
    state: () => ({ 
        token:"",
        userinfo:"",
        agent:"Win-Client",
        outputDeviceId: "",
        apiHost: "http://183.221.0.153:35009/",
        orgInfo: null,
        orgId:"",
        clientId:"",
        demoMode:false,//主题演示主题
        bannerAuto:true,//是否自动滚动,默认自动滚动
        themeCache:false,//
        theme:'customLightTheme',//当前主题
        orgList:[]
    }),
    getters: {
        isLogin: state => !!state.token,
    },
    actions: {
        setAgent(agent) {
            this.agent = agent
        },
        setApiHost(apiHost) {
            this.apiHost = apiHost
        },
        setOutputDeviceId(outputDeviceId) {
            this.outputDeviceId = outputDeviceId
        },
        setOrgId(str) {
            this.orgId = str
        },
        setClientId(str) {
            this.clientId = str
        },
        setOrgInfo(data) {
            this.orgInfo = data
        },
        setUserToken(str) {
            this.token = str
        },
        setUserInfo(info) {
            this.userinfo = info
        },  
        setDemoMode(value) {
            this.demoMode = value
        },
        setTheme(value) {
            this.theme = value
        },
        setBannerAuto(value) {
            this.bannerAuto = value
        },
        setThemeCache(value) {
            this.themeCache = value
        },
        setOrgId(str) {
            this.orgId = str
        },
        setOrgList(list) {
            this.orgList = list
        },
        logout() {
            this.setUserToken("")
            this.setUserInfo({})
            this.setOrgId("")
            this.setOrgInfo(null) 
            this.setOrgList([])  
         },
        async getUserInfoData() {
            try {
                const res = await getMyUser()
                if(res.data){
                    this.setUserInfo(res.data.unifiedUser)
                    if(res.data.orgList&&res.data.orgList.length>0){ 
                        this.setOrgId(res.data.orgList[0].id)
                        this.setOrgInfo(res.data.orgList[0])
                        this.setOrgList(res.data.orgList)  
                    }
                }else{
                    this.setUserInfo({})
                    this.setOrgId("")
                    this.setOrgInfo(null) 
                    this.setOrgList([])  
                }
                 return res.data
            } catch (error) {
                throw error
            }
        },
    }
},{ plugins: [createLocalStoragePlugin()],})